package com.example.config.datasource;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import com.example.config.intercept.CoreDynamicSqlInterceptor;
import com.example.config.intercept.DynamicSqlInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

import static com.example.config.constant.ConstantConfig.SQL_SESSION_FACTORY_BEAN_NAME;

@Configuration
@MapperScan(basePackages = "com.example.mapper.starrocks",sqlSessionFactoryRef = SQL_SESSION_FACTORY_BEAN_NAME)
public class StarRocksDataSourceConfig {
    // 注入 MyBatisPlus 的配置属性
    @Bean("starrocksMybatisPlusConfig")
    @ConfigurationProperties(prefix = "mybatis-plus.configuration.starrocks-log-config")
    public MybatisConfiguration globalConfiguration() {
        return new MybatisConfiguration();
    }

    @Bean(name = "starRocksDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.star-rocks-db")
    public DataSource starRocksDataSource() {
        return DataSourceBuilder.create().build();
    }


    @Bean(name = SQL_SESSION_FACTORY_BEAN_NAME)
    public SqlSessionFactory sqlSessionFactory(@Qualifier("starRocksDataSource") DataSource dataSource,
                                               @Qualifier("starrocksMybatisPlusConfig")MybatisConfiguration config) throws Exception {
        MybatisSqlSessionFactoryBean starRocksSqlSessionFactory = new MybatisSqlSessionFactoryBean();
        starRocksSqlSessionFactory.setDataSource(dataSource);
        starRocksSqlSessionFactory.setConfiguration(config);

        CoreDynamicSqlInterceptor coreDynamicSqlInterceptor = new CoreDynamicSqlInterceptor();


        DynamicSqlInterceptor dynamicSqlInterceptor = new DynamicSqlInterceptor();
        // 设置 MyBatis-Plus 分页插件
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(dynamicSqlInterceptor);
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//如果配置多个插件,切记分页最后添加
        starRocksSqlSessionFactory.setPlugins(coreDynamicSqlInterceptor,interceptor);
//        starRocksSqlSessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/two/*.xml"));
        return starRocksSqlSessionFactory.getObject();
    }

    @Bean(name = "starRocksTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("starRocksDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "starRocksSqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier(SQL_SESSION_FACTORY_BEAN_NAME) SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }



}
